<template>
  <div class="vue-box">
    <div class="c-panel">
      <!-- ------------- 检索参数 ------------- -->
      <h4 class="c-title">检索参数</h4>
      <el-form @submit.native.prevent class="conditional_filter">
        <sa-item v-model="p.search" type="text" name="模糊搜索" />
        <sa-item v-model="p.status" type="enum" jtype="4" :jv="dims.YES_OR_NO" name="启用状态" :def="{}"
          :clearable="true" />
        <sa-item v-model="p.commonFlag" type="enum" jtype="4" :jv="dims.YES_OR_NO" name="公用标识" :def="{}"
          :clearable="true" />
        <div class="c-item">
          <el-button type="primary" icon="el-icon-search" @click="p.current = 1; f5()">查询</el-button>
        </div>
        <br>
      </el-form>
      <!-- ------------- 快捷按钮 ------------- -->
      <sa-item type="fast-btn" show="add,get,delete,export,reset" />
      <!-- ------------- 数据列表 ------------- -->
      <el-table ref="data-table" class="data-table" :data="dataList" border highlight-current-row stripe row-key="id"
        :tree-props="{ children: 'children' }" @expand-change="sa.f5TableHeight()">
        <sa-td type="selection" width="60px" />
        <sa-td type="text" name="序号" prop="sort" />
        <sa-td type="text" name="菜单名" prop="name" />
        <sa-td type="text" name="图标" prop="icon" />
        <sa-td type="text" name="识别码" prop="code" />
        <sa-td type="text" name="访问地址" prop="path" />
        <sa-td type="text" name="组件" prop="component" />

        <sa-td type="enum" name="固定位置" prop="affix" :jv="dims.YES_OR_NO" />
        <sa-td type="enum" name="隐藏标识" prop="hidden" :jv="dims.YES_OR_NO" />
        <sa-td type="enum" name="权限标识" prop="isAuth" :jv="dims.YES_OR_NO" />
        <sa-td type="enum" name="菜单等级" prop="level" :jv="dims.MENU_LEVEL" />
        <sa-td type="enum" name="显示菜单" prop="alwaysShow" :jv="dims.YES_OR_NO" />

        <sa-td type="enum" name="公用标识" prop="commonFlag" :jv="dims.YES_OR_NO" />
        <sa-td type="enum" name="启用状态" prop="status" :jv="dims.YES_OR_NO" />
        <sa-td type="datetime" name="创建时间" prop="createTime" width="140px" />
        <sa-td name="操作" fixed="right" width="160px">
          <template slot-scope="s">
            <div class="sm">
              <el-button type="info" circle @click="update(s.row)">
                <i class="el-icon-edit-outline"></i>
              </el-button>
              <el-button type="danger" circle @click="del(s.row)">
                <i class="el-icon-delete-solid"></i>
              </el-button>

              <el-button type="primary" circle @click="addSubMenu(s.row)" v-if="s.row.level === 0">
                <i class="el-icon-plus"></i>
              </el-button>
            </div>
          </template>
        </sa-td>
      </el-table>
      <!-- 分页 -->
      <div v-if="dataCount">
        <sa-item type="page" :curr.sync="p.current" :size.sync="p.size" :total="dataCount" @change="f5()" />
      </div>
    </div>

  </div>
</template>

<script>
export default {
  name: 'server-menu',
  data() {
    return {
      // 查询参数
      p: {
        //模糊查询
        current: 1,
        size: 10,
        search: null,
        status: null,
        sortField: null
      },
      dataCount: null,
      // 数据集合
      dataList: [],
      // 缓存字典
      dims: {},
    }
  },
  created: function () {
    this.dims = sa.$sys.getCurd()
    this.f5();
    // 监听回车执行查询
    sa.onInputEnter();
  },
  methods: {
    // 刷新
    f5: function () {
      sa.ajax('/menu/getList', this.p, function (res) {
        // 数据
        this.dataList = res.data;
        this.dataCount = res.dataCount;
        // 刷新表格高度
        sa.f5TableHeight();
      }.bind(this));
    },

    // 新增
    add: function () {
      sa.showModel('新增', () => import('./add'), { dims: this.dims });
    },

    // 变更
    update: function (data) {
      data.dims = this.dims
      sa.showModel('【' + data.name + '】· 变更', () => import('./update'), data);
    },

    // 新增子菜单
    addSubMenu: function (data) {
      sa.showModel('【' + data.name + '】· 新增下级菜单', () => import('./add'), { pid: data.id, dims: this.dims });
    },

    // 删除
    del: function (data) {
      sa.confirm('是否删除，此操作不可撤销', function () {
        sa.ajax('/menu/delete', { id: data.id }, function (res) {
          sa.ok('删除成功');
          this.f5();
        }.bind(this), { type: "get" })
      }.bind(this));
      // 刷新表格高度
      sa.f5TableHeight();
    },

    // 批量删除
    deleteByIds: function () {
      // 获取选中元素的id列表
      let selection = this.$refs['data-table'].selection;
      let ids = sa.getArrayField(selection, 'id');
      if (selection.length == 0) {
        return sa.msg('请至少选择一条数据')
      }
      // 提交删除
      sa.confirm('是否批量删除选中数据？此操作不可撤销', function () {
        sa.ajax('/menu/deleteByIds', { ids: ids }, function (res) {
          sa.ok('删除成功');
          this.f5();
        }.bind(this))
      }.bind(this));
      // 刷新表格高度
      sa.f5TableHeight();
    },

    // 查看详情
    getInfo: function (data) {
      sa.showModel('详情', () => import('./info'), { id: data.id, dims: this.dims });
    },

    // 查看 - 根据选中的
    getBySelect: function (data) {
      var selection = this.$refs['data-table'].selection;
      if (selection.length === 0) {
        return sa.msg('请至少选择一条数据')
      }
      this.getInfo(selection[0]);
    },
  }
}
</script>

<style scoped>
.sm {
  width: 100%;
  height: 100%;

  .el-button {
    border-radius: 50px !important;
    margin: 0px 5px;

    i {
      font-size: xx-small;
    }
  }
}
</style>
